package com.bs.visitservice.controller.seller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.bs.visitservice.entity.Comment;
import com.bs.visitservice.entity.CommentPhoto;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.service.CommentPhotoService;
import com.bs.visitservice.service.CommentService;
import com.bs.visitservice.service.SellerService;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import com.bs.visitservice.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/seller")
public class SellerCommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private CommentPhotoService commentPhotoService;
    @Autowired
    private SellerService sellerService;

    /**
     * 查询店铺，排除已删除状态为4
     * @param cond
     * @return
     */
    @RequestMapping("/querySellerComments")
    public Result querySellerComments(@RequestBody Map cond){
        Long userNo =(Long) ThreadLocalUtil.get("loginNo");
        LambdaQueryWrapper<Seller> wrapperSeller = new LambdaQueryWrapper<>();
        wrapperSeller.eq(Seller::getLoginNo,userNo).ne(Seller::getStatus,4);
        //暂时只支持一个账号开一家店铺
        List<Seller> sellerList = sellerService.list(wrapperSeller);
        if(sellerList.size() < 1){
            return Result.success(new ArrayList<Map<String,Object>>());
        }
        Seller seller = sellerList.get(0);
        if(cond.get("pageIndex")!=null && cond.get("pageSize")!=null){
            Integer pageIndex = (Integer)cond.get("pageIndex");
            Integer pageSize = (Integer)cond.get("pageSize");
            cond.put("pageIndex",(pageIndex-1)*pageSize);
        }
        cond.put("sellerNo",seller.getSellerNo());
        List<Map<String, Object>> sellerComments = commentService.querySellerComments(cond);
        Map<String,Object> cond1 = new HashMap<>();
        cond1.put("sellerNo",seller.getSellerNo());
        Integer total = commentService.querySellerComments(cond1).size();
        sellerComments.forEach(map->{
            Long no = (Long)map.get("no");
            LambdaQueryWrapper<CommentPhoto> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommentPhoto::getCommentNo,no).orderByAsc(CommentPhoto::getSn);
            List<Map<String, Object>> photoList = commentPhotoService.listMaps(wrapper);
            map.put("photos",photoList);
        });
        Map<String,Object> result = new HashMap<>();
        result.put("datas",sellerComments);
        result.put("total",total);
        return Result.success(result);
    }

    /**
     * 差评删除申请
     * @param cond
     * @return
     */
    @RequestMapping(value = "/negCommentApply",method = RequestMethod.POST)
    public Result negCommentApply(@RequestBody Map cond){
      Comment comment =  (Comment)ResultUtil.transObject(cond, Comment.class);
      comment.setFlag("1");
      boolean isSuc = commentService.updateById(comment);
      if(isSuc){
          return Result.success(null);
      }
      return Result.error(null);
    }
}
